<?php
/**
 * ============================================================================
 * * COPYRIGHT 2014-2018 UKEBAN.NET , and all rights reserved.
 * * WEBSITE: http://www.ukeban.net;
 * ----------------------------------------------------------------------------
 * This is not a free software!You have not used for commercial purposes in the
 * premise of the program code to modify and use; and publication does not allow
 * any form of code for any purpose.
 * ============================================================================
 * filename: OrdersAction.class.php  2016年7月20日 下午2:20:12  binghuo
 * phone: 18986078695 QQ: 282818018
 */
class WxNotifyAction extends Action
{
    //微信支付回调
    public function wxnotify(){
		$postXml = $GLOBALS["HTTP_RAW_POST_DATA"];
		if (empty($postXml)) {
		    return false;
		}
		$attr = $this->xmlToArray($postXml);
		log::write('微信支付返回：'.print_r($attr,1));
		if(array_key_exists("return_code", $attr)&& array_key_exists("result_code", $attr) && $attr["return_code"] == "SUCCESS" && $attr["result_code"] == "SUCCESS"){
			$arr = explode('|',$attr['attach']);
			if($arr[0] == 'tsd'){
				$tsd_tmp = explode('_',$arr[1]);
				if(count($tsd_tmp)>1){
					$maps['idx'] = array('in',$tsd_tmp);
					$orders = M('orders_transactions')->where($maps)->field('idx,order_idx')->select();
					foreach($orders as $v){
						$map['idx'] = $v['idx'];
						$dts['gateway_trans_id'] = $attr['transaction_id'];
						$dts['status'] = '1';
						$dts['note'] = '付款成功';
						$dts['datetime'] = time();
						$res = M('orders_transactions')->where($map)->data($dts)->save();
						
						$map3['order_idx'] = $v['order_idx'];
						$dts3['order_status'] = '10';
						$dts3['edit_time'] = time();
						$res3 = M('orders')->where($map3)->data($dts3)->save();
						
						$dts2['order_idx'] = $v['order_idx'];
				        $dts2['change_before'] = '00';
				        $dts2['change_after'] = '10';
				        $dts2['adminer'] = '';
				        $dts2['note'] = '微信支付';
				        $dts2['datetime'] = time();
				        $res2 = M('orders_changelog')->data($dts2)->add();
					}
				}else{
					$maps['idx'] = $arr[1];
					$orders = M('orders_transactions')->where($maps)->field('idx,order_idx')->find();
					
					$map['idx'] = $arr[1];
					$dts['amount'] = $attr['total_fee']/100;
					$dts['gateway_trans_id'] = $attr['transaction_id'];
					$dts['status'] = '1';
					$dts['note'] = '付款成功';
					$dts['datetime'] = time();
					$res = M('orders_transactions')->where($map)->data($dts)->save();
					
					$map3['order_idx'] = $orders['order_idx'];
					$dts3['order_status'] = '10';
					$dts3['edit_time'] = time();
					$res3 = M('orders')->where($map3)->data($dts3)->save();
					
					$dts2['order_idx'] = $orders['order_idx'];
			        $dts2['change_before'] = '00';
			        $dts2['change_after'] = '10';
			        $dts2['adminer'] = '';
			        $dts2['note'] = '微信支付';
			        $dts2['datetime'] = time();
			        $res2 = M('orders_changelog')->data($dts2)->add();
				}
			}
			echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
			return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
		}else{
			echo '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
			return '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
		}
    }
	//将xml格式转换成数组
	function xmlToArray($xml) {
		libxml_disable_entity_loader(true);
		$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
		$val = json_decode(json_encode($xmlstring), true);
		return $val;
	}
}